package cn.youyeku.sockettransmission.utils;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Environment;

import java.io.File;
import java.io.IOException;

import cn.youyeku.sockettransmission.ui.ConstantAppConfig;

/**
 * Created by youye on 17-6-1.
 */


//特殊类名 使用SysApp
public class SysApplication extends Application {


    public static Context AppContext;


    public SysApplication() {
        super();
    }

    @Override
    public void onCreate() {

        super.onCreate();
        AppBaseContext = getBaseContext();
        AppContext = getApplicationContext();




        ConstantAppConfig.readWriteXml = new ReadWriteXml();
        //初始化
        ConstantAppConfig.mSharedPreferences = getSharedPreferences("Config",
                Context.MODE_PRIVATE);
        ConstantAppConfig.mEditor = ConstantAppConfig.mSharedPreferences.edit();


        //读取配置
        LoadConfig();

    }

    private void LoadConfig() {

        File f = new File(getFilesDir(),"FIRST");

        //第一次配置
        if(!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                LogUtils.w("创建文件ERROR");
                e.printStackTrace();
            }


            //初始化第一次加载
            ConstantAppConfig.IP="192.168.1.101";
            ConstantAppConfig.PORT=25565;
            ConstantAppConfig.PATH=getSDcardPath();
            ConstantAppConfig.readWriteXml.writeString("IP",ConstantAppConfig.IP).
                    writeInt("PORT",ConstantAppConfig.PORT)
                    .writeString("PATH",ConstantAppConfig.PATH).commit();


        }else {
            //读取配置

            ConstantAppConfig.IP=ConstantAppConfig.readWriteXml.getString("IP","");
            ConstantAppConfig.PORT=ConstantAppConfig.readWriteXml.getInt("PORT",25565);
            ConstantAppConfig.PATH=ConstantAppConfig.readWriteXml.getString("PATH","");

        }

        ConstantAppConfig.SAVE_PATH = ConstantAppConfig.PATH;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();


    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }


    //todo setter getter
    public static Context getAppBaseContext() {
        return AppBaseContext;
    }

    public static Context AppBaseContext;

    public static Context getAppContext() {
        return getAppContext();
    }

    public String getSDcardPath() {
        return Environment.getExternalStorageDirectory().getPath();
    }


}
